home *** CD-ROM | disk | FTP | other *** search
- Path: airdmhor.gen.nz!not-for-mail
- From: gumboot@airdmhor.gen.nz (Simon Hosie)
- Newsgroups: comp.lang.c
- Subject: Re: division problem
- Date: 3 Feb 1996 06:59:15 +1300
- Organization: Airdmhor
- Message-ID: <4etjdj$fil@airdmhor.gen.nz>
- References: <31097D77.11AA@rain.org> <26JAN199622082450@erich.triumf.ca> <4eh246$u6h@airdmhor.gen.nz> <4ej4ha$66@fountain.mindlink.net> <DLzvGG.2rn@uns.bris.ac.uk> <Pine.SOL.3.90.960130150923.21923F-100000@flute>
- NNTP-Posting-Host: localhost.gen.nz
- X-Newsreader: TIN [version 1.2 PL2]
-
- On Tue, 30 Jan 1996, Nathan Sidwell wrote:
- > Gene Wirchenko (genew@mindlink.bc.ca) wrote:
- > : gumboot@airdmhor.gen.nz (Simon Hosie) wrote:
- >
- > : >celcius = (fahrenheit - 32) * 5 / 9;
- >
- > : That's disgusting <g> as it doesn't round. Try it with 39 deg F:
- > : (39-32)*5/9 ::= 7*5/9 ::= 35/9 ::= 3
- > : but the actual value is 3.8... i.e. nearly 4. If you must int, 4
- > : would be a better answer.
- >
- > Still no need to use floating point,
- > celcius = ((fahrenheit - 32) * 5 + 4) / 9
-
- Darrell Grainger:
- > This ALMOST does the trick but 4/9 = 0.44444444... and 5/9 = 0.555555....
- > What you really want to do to eliminate the rounding errors is add 0.5 to
- > the number, e.g. 1.49999... + 0.5 = 1.9999... and (int)1.9999... = 1. So
- > 1.49999... rounds down to 1. Then 1.5 + 0.5 = 2.0 and (int)2.0 = 2. So
- > 1.5 rounds up to 2. This is what most people consider correct rounding.
-
- > In your 'trick' you would have 1.5 + 0.4444... = 1.9444... and
- > (int)1.9444... = 1. This means that 1.5 rounds down to 1. This is not
- > proper. It is close but not proper. Alternatively, adding 5/9th would round
- > up certain numbers it should not.
-
- Hello? Can anybody hear me? Ok, let's try again...
-
- #if -3 / 2 == -2
- celcius = ((fahrenheit - 32) * 10 + 9) / 18;
- #else
- celcius = ((fahrenheit - 32) * 10 + ((fahrenheit - 32 >= 0) ? 9 : -9))
- / 18;
- #endif
-
- There.. complete, portable, accurate rounding.
-